package com.qingcloud.adminbackend.service.impl;

import com.qingcloud.adminbackend.constant.WamingConstant;
import com.qingcloud.adminbackend.base.AjaxResult;
import com.qingcloud.adminbackend.base.Vuetable2PageData;
import com.qingcloud.adminbackend.entity.upgrade.UpgradeSpecialHisDTO;
import com.qingcloud.adminbackend.entity.upgrade.UpgradeSpecialRequest;
import com.qingcloud.adminbackend.mapper.ITdUpgradeSpecialHisMapper;
import com.qingcloud.adminbackend.service.ITdUpgradeSpecialHisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ITdUpgradeSpecialHisServiceImpl implements ITdUpgradeSpecialHisService {

    @Resource
    private ITdUpgradeSpecialHisMapper upgradeSpecialHisMapper;

    /**
     * 分页查询
     *
     * @param upgradeSpecialRequest
     * @return
     */
    public AjaxResult getUpgradeSpecialHis(UpgradeSpecialRequest upgradeSpecialRequest) {
        List<UpgradeSpecialHisDTO> usList = upgradeSpecialHisMapper.search(upgradeSpecialRequest, upgradeSpecialRequest.getOffset(), upgradeSpecialRequest.getPerPage());
        Integer count = upgradeSpecialHisMapper.count(upgradeSpecialRequest);
        Map<String, Object> resultMap = new HashMap<>(2);
        resultMap.put("data", usList);
        resultMap.put("pagination", new Vuetable2PageData(count.longValue(), upgradeSpecialRequest.getPerPage(), upgradeSpecialRequest.getPage()));
        return new AjaxResult(WamingConstant.SUCCESS, null, resultMap);
    }

    /**
     * 添加
     *
     * @param upgradeSpecialHis
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult addUpgradeSpecialHis(UpgradeSpecialHisDTO upgradeSpecialHis) {
        Integer flag = upgradeSpecialHisMapper.save(upgradeSpecialHis);
        if (flag > 0) {
            return new AjaxResult(WamingConstant.SUCCESS, null, null);
        }
        return new AjaxResult(WamingConstant.FAILURE, null, null);
    }

    /**
     * 通过ID查找
     *
     * @param id
     * @return
     */
    public AjaxResult getUpgradeSpecialHisById(String id) {
        UpgradeSpecialHisDTO us = upgradeSpecialHisMapper.findUpgradeSpecialHisById(id);
        return new AjaxResult(WamingConstant.SUCCESS, null, us);
    }

    /**
     * 更新
     *
     * @param upgradeSpecialHis
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateUpgradeSpecialHis(UpgradeSpecialHisDTO upgradeSpecialHis) {
        Integer flag = upgradeSpecialHisMapper.update(upgradeSpecialHis);
        if (flag > 0) {
            return new AjaxResult(WamingConstant.SUCCESS, null, null);
        }
        return new AjaxResult(WamingConstant.FAILURE, null, null);
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult deleteUpgradeSpecialHis(String[] ids) {
        List<String> idList = Arrays.asList(ids);
        Integer flag = upgradeSpecialHisMapper.deleteByIdList(idList);
        if (flag > 0) {
            return new AjaxResult(WamingConstant.SUCCESS, "删除成功", null);
        }
        return new AjaxResult(WamingConstant.FAILURE, "删除失败", null);
    }

}
